<template>
  <div class="app-container">
    <div class="app-container-main">
      <div class="app-container-main-col flex-1">
        <filter-config-box
          :options="filterOptions"
          v-model="queryParams"
          @search="handleQuery"
          @onReset="resetQuery"
        />
        <el-card class="table-card">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd()"
                >新增</el-button
              >
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                >删除</el-button
              >
              <span style="margin-left: 10px; color: red; font-weight: bold"
                >当月花销总金额：{{ totalActual }}</span
              >
            </el-col>
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </el-row>

          <el-table
            ref="tables"
            v-loading="loading"
            :data="list"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" type="index" align="center">
              <template slot-scope="scope">
                <span>{{
                  (queryParams.pageNum - 1) * queryParams.pageSize +
                  scope.$index +
                  1
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="账单编号"
              :show-overflow-tooltip="true"
              align="center"
              prop="billId"
            />
            <el-table-column
              label="账单日期"
              align="center"
              prop="spendingTime"
            />
            <el-table-column
              label="账单金额"
              align="center"
              prop="spendingPrice"
            >
              <template #default="{ row }">
                {{ "￥" + row.spendingPrice + "元" }}
              </template>
            </el-table-column>
            <el-table-column label="用户名称" align="center" prop="createBy" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column
              label="创建时间"
              align="center"
              prop="createTime"
            />
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handlePreview(scope.row)"
                  >详情</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
            <div slot="empty" class="emptyBg">
              <el-empty :image-size="200"></el-empty>
            </div>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </div>
    </div>
    <!-- 添加和修改 -->
    <add-and-edit-bill ref="addAndEditBill" :success="success" />
    <!-- 列表详情 -->
    <preview ref="preview" />
  </div>
</template>

<script>
import { list, getBill, delBill, getTotalActual } from "@/api/bill/billList.js";
import addAndEditBill from "./addAndEditBill.vue";
import preview from "./preview.vue";

export default {
  name: "BillList",
  components: { addAndEditBill, preview },
  data() {
    return {
      totalActual: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 日期范围
        dateRange: [],
        userName: undefined,
      },
    };
  },
  created() {},
  mounted() {
    if (this.$route.params.depositTime) {
      const year = new Date(this.$route.params.depositTime).getFullYear();
      const month = new Date(this.$route.params.depositTime).getMonth() + 1;
      // 开始时间
      const begin =
        this.$route.params.depositTime +
        "-" +
        new Date(year, month, 1).getDate() +
        " 00:00:00";
      // 结束时间
      const end =
        this.$route.params.depositTime +
        "-" +
        new Date(year, month, 0).getDate() +
        " 23:59:59";
      this.queryParams.dateRange = [begin, end];
      this.getList();
    } else {
      this.getList();
    }
  },
  methods: {
    // 获取实际金额
    async getActualAmount() {
      let data = {};
      if (this.queryParams.dateRange.length > 0) {
        data = {
          beginTime: this.queryParams.dateRange[0],
          endTime: this.queryParams.dateRange[1],
        };
      } else {
        // 获取当月的第一天和最后一天
        const year = new Date().getFullYear();
        const month = new Date().getMonth() + 1;
        const beginTime = year + "-" + month + "-01";
        const endTime =
          year + "-" + month + "-" + new Date(year, month, 0).getDate();
        data = {
          beginTime,
          endTime,
        };
      }
      const res = await getTotalActual(data);
      this.totalActual = res.data.totalActual;
    },
    /** 详情 */
    handlePreview(row) {
      getBill(row.id).then((response) => {
        this.$refs.preview.open(response.data);
      });
    },
    /** 成功后的回调 */
    success() {
      this.getList();
    },
    /** 添加 */
    handleAdd() {
      this.$refs.addAndEditBill.open("add", {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      getBill(row.id).then((response) => {
        this.$refs.addAndEditBill.open("update", response.data);
      });
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      list(this.insertDateRange(this.queryParams)).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.getActualAmount();
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除访问编号为"' + billIds + '"的数据项？')
        .then(function () {
          return delBill(billIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
  computed: {
    getRoles() {
      return this.$store.state.user.roles;
    },
    filterOptions() {
      return [
        {
          prop: "createBy",
          label: "用户名称",
          labelWidth: "68px",
          showTag: this.getRoles[0] === "admin" ? "" : "showTag",
          attrs: {
            style: {
              width: "240px",
            },
          },
        },
        {
          tag: "daterange",
          prop: "dateRange",
          label: "账单日期",
          labelWidth: "68px",
          attrs: {
            style: {
              width: "240px",
            },
            "default-time": ["00:00:00", "23:59:59"],
          },
        },
      ];
    },
  },
};
</script>

